const mongoose = require('./model/db');
const { Course } = require('./model/model');

async function simpleTest() {
  try {
    console.log('🔍 简单课程API测试...');
    
    // 等待数据库连接
    await new Promise(resolve => {
      if (mongoose.connection.readyState === 1) {
        resolve();
      } else {
        mongoose.connection.once('connected', resolve);
      }
    });
    
    console.log('✅ 数据库连接成功');
    
    // 尝试获取课程数量
    const count = await Course.countDocuments();
    console.log(`✅ 数据库中有 ${count} 个课程`);
    
    // 尝试获取一个课程
    const course = await Course.findOne();
    if (course) {
      console.log(`✅ 成功获取课程: ${course.name}`);
    } else {
      console.log('⚠️ 数据库中没有课程数据');
    }
    
    console.log('✅ 测试完成');
    
  } catch (error) {
    console.error('❌ 测试失败:', error.message);
    console.error('错误堆栈:', error.stack);
  } finally {
    await mongoose.connection.close();
    console.log('🔹 数据库连接已关闭');
  }
}

simpleTest();
